package com.example.langchain4j.controller;

import com.example.langchain4j.service.ChatService;
import com.example.langchain4j.utils.DocumentUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/xf")
public class XiaofangController {
    
    @Autowired
    private ChatService chatService;

    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadDocument(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseEntity.badRequest()
                    .body(Map.of("error", "File is empty"));
            }

            String fileName = file.getOriginalFilename();

            String extractedText = DocumentUtils.extractTextFromWord(file);

            return ResponseEntity.ok(Map.of(
                "success", true,
                "filename", fileName,
                "content", extractedText,
                "contentLength", extractedText.length()
            ));

        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                .body(Map.of("error", "Failed to process document: " + e.getMessage()));
        }
    }

    @PostMapping("/generation-table")
    public void generationTable(
            @RequestParam("file1") MultipartFile file1,
            @RequestParam("file2") MultipartFile file2,
            HttpServletResponse  response) {
            // 验证文件
//            if (file1.isEmpty() || file2.isEmpty()) {
//                return ResponseEntity.badRequest()
//                    .body(Map.of("error", "Both files are required"));
//            }
//
//            String fileName1 = file1.getOriginalFilename();
//            String fileName2 = file2.getOriginalFilename();
//
//            if (!documentService.isWordDocument(fileName1) || !documentService.isWordDocument(fileName2)) {
//                return ResponseEntity.badRequest()
//                    .body(Map.of("error", "Only Word documents (.doc, .docx) are supported"));
//            }

            chatService.generationShenpibiao(file1, file2, response);

//            return ResponseEntity.ok(Map.of(
//                "success", true,
//                "document1", jsonResult
//            ));
    }










}